package com.bird.thread.base.background;

import java.util.concurrent.TimeUnit;

/**
 * @author: bird
 * @Data: 2021/7/5 15:46
 * @description: 验证：初始化未完成 就使用this进行赋值 导致线程安全的问题
 */
 public class InitializationNotComplete {

    public static Point point;

    public static void main(String[] args) throws InterruptedException {
        new PointMark().start();
        //如果这个时间 设置的太短 就会 出现两次结果不一致的问题
        TimeUnit.MILLISECONDS.sleep(100);
        if (point != null) {
            System.out.println(point);
        }
    }
}


class Point {
    private final int x, y;

    public Point(int x, int y) throws InterruptedException {
        this.x = x;
        InitializationNotComplete.point = this;
        TimeUnit.MILLISECONDS.sleep(100);
        this.y = y;
    }

    @Override
    public String toString() {
        return x + "," + y;
    }
}


class PointMark extends Thread {
    @Override
    public void run() {
        try {
            new Point(1, 1);
            TimeUnit.MILLISECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

